package com.imau.brand.controller;

import com.alibaba.fastjson.JSON;
import com.imau.brand.bean.Brand;
import com.imau.brand.service.BrandService;
import com.imau.brand.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/brandAll")
public class BrandAllController extends HttpServlet {

    /**初始化业务层对象*/
    private BrandService brandService = new BrandServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.查询所有品牌数据
        List<Brand> brands = brandService.findAll();
        //2.转成JSON
        String jsonString = JSON.toJSONString(brands);
        //3.响应客户端
        response.getWriter().write(jsonString);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
